package com.uzai.console.service.wechatprivate.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.dto.device.*;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.device.deviceauth.DeviceAuthQuery;
import com.uzai.console.entity.*;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.mapper.CdkeysMapper;
import com.uzai.console.mapper.DeviceAuthDetailMapper;
import com.uzai.console.mapper.DeviceAuthMapper;
import com.uzai.console.service.feign.ClientApiService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoCardPayService;
import com.uzai.console.service.wechatprivate.DeviceWechatInfoSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 机器人
 *
 * @author liuqi
 * @date 2021年08月19日 16:50
 */
@Service
public class DeviceWechatInfoCardPayServiceImpl implements DeviceWechatInfoCardPayService {

    Logger logger = LoggerFactory.getLogger(DeviceWechatInfoCardPayServiceImpl.class);

    @Autowired
    private CdkeysMapper cdkeysMapper;
    @Autowired
    private DeviceAuthMapper deviceAuthMapper;
    @Autowired
    private DeviceAuthDetailMapper deviceAuthDetailMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DeviceWechatInfoSyncService deviceWechatInfoSyncService;
    @Autowired
    private ClientApiService clientApiService;

    /**
     * 购买机器人授权-单个卡支付
     *
     * @param deviceWechatInfo
     * @author liuqi
     * @date 2021/8/25 11:38
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buyDeviceAuthByCard(DeviceWechatInfo deviceWechatInfo, Integer deviceTypeId, Cdkeys cdkeys, Integer now, String trandeNo, List<String> productCodeList) {

        //修改卡密状态为使用
        cdkeys.setStatus(CdkeysStatusEnum.USED.getId());
        cdkeys.setUpdateTime(now);
        cdkeys.setTradeno(trandeNo);
        cdkeys.setUseTime(now);
        int i = cdkeysMapper.updateById(cdkeys);

        if (i == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "修改卡密失败");
        }

        //卡授权类型
        Integer authType_card = 1;
        //时长
        Integer quantity_card = 0; //卡密支付，时长从卡信息获取
        Integer beginTime_card = now;
        Integer expireTime_card = now;
        if (cdkeys.getType().intValue() == 1) {//月卡
            quantity_card = 1;
            authType_card = DeviceAuthTypeEnum.DAT_MONTH.getId();
        } else if (cdkeys.getType().intValue() == 2) { //年卡
            quantity_card = 12;
            authType_card = DeviceAuthTypeEnum.DAT_YEAR.getId();
        }

        for (String productCode : productCodeList) {
            //添加机器人授权表-如果没有则新增，有则修改
            DeviceAuth deviceAuth = deviceAuthMapper.selectByDeviceIdAndProductCode(deviceWechatInfo.getDeviceId(), productCode);
            if (deviceAuth != null) { //修改
                //判断旧到期时间是否过期
                Integer old_expireTime = Tools.getInteger(deviceAuth.getExpireTime());
                if (old_expireTime <= now) {//已经到期，则从今天开始算
                    beginTime_card = now;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                } else {
                    beginTime_card = old_expireTime;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                }
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setUpdateTime(now);
                deviceAuth.setAuthType(authType_card);
                deviceAuthMapper.updateById(deviceAuth);
            } else {
                deviceAuth = new DeviceAuth();
                deviceAuth.setId(IdWorker.getId());
                deviceAuth.setDeviceId(deviceWechatInfo.getDeviceId());
                deviceAuth.setDeviceUniqueId(deviceWechatInfo.getId());
                deviceAuth.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
                deviceAuth.setDeviceTypeId(deviceTypeId);
                deviceAuth.setAuthType(authType_card);
                deviceAuth.setProductCode(productCode);
                beginTime_card = now;
                expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setCreateTime(now);
                deviceAuth.setUpdateTime(now);
                deviceAuthMapper.insert(deviceAuth);
            }

            //添加机器人授权明细表
            DeviceAuthDetail deviceAuthDetail = new DeviceAuthDetail();
            deviceAuthDetail.setId(IdWorker.getId());
            deviceAuthDetail.setMerId(deviceWechatInfo.getMerId());
            deviceAuthDetail.setDeviceUniqueId(deviceWechatInfo.getId());
            deviceAuthDetail.setDeviceId(deviceWechatInfo.getDeviceId());
            deviceAuthDetail.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
            deviceAuthDetail.setDeviceTypeId(deviceTypeId);
            deviceAuthDetail.setProductCode(productCode);
            deviceAuthDetail.setAuthType(authType_card);
            deviceAuthDetail.setQuantity(quantity_card);
            deviceAuthDetail.setBeginTime(beginTime_card);
            deviceAuthDetail.setExpireTime(expireTime_card);
            deviceAuthDetail.setPayTime(now);
            deviceAuthDetail.setPayType(PaymentTypeEnum.PAY_CARDID.getId());
            deviceAuthDetail.setTradeno(trandeNo);
            deviceAuthDetail.setAmount(0.00D);
            deviceAuthDetail.setRemark("卡密支付");
            deviceAuthDetail.setCardId(cdkeys.getCard());
            deviceAuthDetail.setCreateTime(now);
            deviceAuthDetail.setUpdateTime(now);
            deviceAuthDetailMapper.insert(deviceAuthDetail);
        }

        //封装机器人授权信息
        List<DeviceInfoBaseCache.DeviceProductAuth> deviceProductAuthList = new ArrayList<>();
        //查询该机器人的所有产品授权信息
        DeviceAuthQuery deviceAuthQuery = new DeviceAuthQuery();
        deviceAuthQuery.setDeviceId(deviceWechatInfo.getDeviceId());
        List<DeviceAuth> deviceAuthList = deviceAuthMapper.findByList(deviceAuthQuery);
        if (deviceAuthList != null && deviceAuthList.size() > 0) {
            for (DeviceAuth deviceAuth : deviceAuthList) {
                //机器人redis缓存信息-产品授权
                DeviceInfoBaseCache.DeviceProductAuth deviceProductAuth = new DeviceInfoBaseCache.DeviceProductAuth();
                deviceProductAuth.setAuthType(deviceAuth.getAuthType());
                deviceProductAuth.setExpireTime(deviceAuth.getExpireTime());
                deviceProductAuth.setProductCode(deviceAuth.getProductCode());
                deviceProductAuthList.add(deviceProductAuth);
            }

            //更新PC端redis
            Object object_pc = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
            if (object_pc != null) { //修改
                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(object_pc.toString(), DeviceInfoPcWeChatCacheDto.class);
                if (deviceInfoPcWeChatCacheDto != null) {
                    deviceInfoPcWeChatCacheDto.setDeviceAuths(deviceProductAuthList);
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoPcWeChatCacheDto)));
            } else {//新增
                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = new DeviceInfoPcWeChatCacheDto();
                deviceInfoPcWeChatCacheDto.setMerId(deviceWechatInfo.getMerId());
                deviceInfoPcWeChatCacheDto.setDeviceId(deviceWechatInfo.getDeviceId());
                deviceInfoPcWeChatCacheDto.setDeviceUniqueId(deviceWechatInfo.getId());
                deviceInfoPcWeChatCacheDto.setConfigId(deviceWechatInfo.getConfigId());
                deviceInfoPcWeChatCacheDto.setDeviceAuths(deviceProductAuthList);
                if (deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT.getId()) { //PC端接收消息
                    deviceInfoPcWeChatCacheDto.setMsgHandler(1); //接收消息
                } else {
                    deviceInfoPcWeChatCacheDto.setMsgHandler(0); //不接受消息
                }
                //更新PC端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoPcWeChatCacheDto)));
            }

            //更新移动端redis
            Object object_moblie = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
            if (object_moblie != null) { //修改
                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(object_moblie.toString(), DeviceInfoMobileWeChatCacheDto.class);
                if (deviceInfoMobileWeChatCacheDto != null) {
                    deviceInfoMobileWeChatCacheDto.setDeviceAuths(deviceProductAuthList);
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoMobileWeChatCacheDto)));

            } else {
                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = new DeviceInfoMobileWeChatCacheDto();
                deviceInfoMobileWeChatCacheDto.setMerId(deviceWechatInfo.getMerId());
                deviceInfoMobileWeChatCacheDto.setDeviceId(deviceWechatInfo.getDeviceId());
                deviceInfoMobileWeChatCacheDto.setDeviceUniqueId(deviceWechatInfo.getId());
                deviceInfoMobileWeChatCacheDto.setConfigId(deviceWechatInfo.getConfigId());
                deviceInfoMobileWeChatCacheDto.setDeviceAuths(deviceProductAuthList);
                if (deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_ANDROID_WECHAT.getId()) { //手机端接收消息
                    deviceInfoMobileWeChatCacheDto.setMsgHandler(1);
                } else {
                    deviceInfoMobileWeChatCacheDto.setMsgHandler(0);
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoMobileWeChatCacheDto)));
            }
        }

        //异步执行给机器人移动端APP授权
        deviceWechatInfoSyncService.mobiledeviceAuthAddSync(deviceWechatInfo.getDeviceId(), deviceWechatInfo.getRecvmsgType());
    }


    /**
     * 购买公众号授权-单个卡支付
     *
     * @param wxpubInfoEntity
     * @author liuqi
     * @date 2021/8/25 11:38
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buyWxpubAuthByCard(WxpubInfoEntity wxpubInfoEntity, Integer deviceTypeId, Cdkeys cdkeys, Integer now, String trandeNo, List<String> productCodeList) {

        //修改卡密状态为使用
        cdkeys.setStatus(CdkeysStatusEnum.USED.getId());
        cdkeys.setUpdateTime(now);
        cdkeys.setTradeno(trandeNo);
        cdkeys.setUseTime(now);
        int i = cdkeysMapper.updateById(cdkeys);

        if (i == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "修改卡密失败");
        }

        //卡授权类型
        Integer authType_card = 1;
        //时长
        Integer quantity_card = 0; //卡密支付，时长从卡信息获取
        Integer beginTime_card = now;
        Integer expireTime_card = now;
        if (cdkeys.getType().intValue() == 1) {//月卡
            quantity_card = 1;
            authType_card = DeviceAuthTypeEnum.DAT_MONTH.getId();
        } else if (cdkeys.getType().intValue() == 2) { //年卡
            quantity_card = 12;
            authType_card = DeviceAuthTypeEnum.DAT_YEAR.getId();
        }

        for (String productCode : productCodeList) {
            //添加机器人授权表-如果没有则新增，有则修改
            DeviceAuth deviceAuth = deviceAuthMapper.selectByDeviceIdAndProductCode(wxpubInfoEntity.getDeviceId(), productCode);
            if (deviceAuth != null) { //修改
                //判断旧到期时间是否过期
                Integer old_expireTime = Tools.getInteger(deviceAuth.getExpireTime());
                if (old_expireTime <= now) {//已经到期，则从今天开始算
                    beginTime_card = now;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                } else {
                    beginTime_card = old_expireTime;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                }
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setUpdateTime(now);
                deviceAuth.setAuthType(authType_card);
                deviceAuthMapper.updateById(deviceAuth);
            } else {
                deviceAuth = new DeviceAuth();
                deviceAuth.setId(IdWorker.getId());
                deviceAuth.setDeviceId(wxpubInfoEntity.getDeviceId());
                deviceAuth.setDeviceUniqueId(wxpubInfoEntity.getId());
                deviceAuth.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
                deviceAuth.setDeviceTypeId(DeviceTypeEnum.WX_PUB.getId());
                deviceAuth.setAuthType(authType_card);
                deviceAuth.setProductCode(productCode);
                beginTime_card = now;
                expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setCreateTime(now);
                deviceAuth.setUpdateTime(now);
                deviceAuthMapper.insert(deviceAuth);
            }

            //添加机器人授权明细表
            DeviceAuthDetail deviceAuthDetail = new DeviceAuthDetail();
            deviceAuthDetail.setId(IdWorker.getId());
            deviceAuthDetail.setMerId(wxpubInfoEntity.getMerId());
            deviceAuthDetail.setDeviceUniqueId(wxpubInfoEntity.getId());
            deviceAuthDetail.setDeviceId(wxpubInfoEntity.getDeviceId());
            deviceAuthDetail.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
            deviceAuthDetail.setDeviceTypeId(DeviceTypeEnum.WX_PUB.getId());
            deviceAuthDetail.setProductCode(productCode);
            deviceAuthDetail.setAuthType(authType_card);
            deviceAuthDetail.setQuantity(quantity_card);
            deviceAuthDetail.setBeginTime(beginTime_card);
            deviceAuthDetail.setExpireTime(expireTime_card);
            deviceAuthDetail.setPayTime(now);
            deviceAuthDetail.setPayType(PaymentTypeEnum.PAY_CARDID.getId());
            deviceAuthDetail.setTradeno(trandeNo);
            deviceAuthDetail.setAmount(0.00D);
            deviceAuthDetail.setRemark("卡密支付");
            deviceAuthDetail.setCardId(cdkeys.getCard());
            deviceAuthDetail.setCreateTime(now);
            deviceAuthDetail.setUpdateTime(now);
            deviceAuthDetailMapper.insert(deviceAuthDetail);

        }

        //封装机器人授权信息
        List<DeviceInfoBaseCache.DeviceProductAuth> deviceProductAuthList = new ArrayList<>();
        //查询该机器人的所有产品授权信息
        DeviceAuthQuery deviceAuthQuery = new DeviceAuthQuery();
        deviceAuthQuery.setDeviceId(wxpubInfoEntity.getDeviceId());
        List<DeviceAuth> deviceAuthList = deviceAuthMapper.findByList(deviceAuthQuery);
        if (deviceAuthList != null && deviceAuthList.size() > 0) {
            for (DeviceAuth deviceAuth : deviceAuthList) {
                //机器人redis缓存信息-产品授权
                DeviceInfoBaseCache.DeviceProductAuth deviceProductAuth = new DeviceInfoBaseCache.DeviceProductAuth();
                deviceProductAuth.setAuthType(deviceAuth.getAuthType());
                deviceProductAuth.setExpireTime(deviceAuth.getExpireTime());
                deviceProductAuth.setProductCode(deviceAuth.getProductCode());
                deviceProductAuthList.add(deviceProductAuth);
            }

            //保存缓存
            Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()));
            if (object != null) { //修改
                DeviceInfoWxpubCacheDto deviceInfoWxpubCacheDto = JSONObject.parseObject(object.toString(), DeviceInfoWxpubCacheDto.class);
                if (deviceInfoWxpubCacheDto != null) {
                    deviceInfoWxpubCacheDto.setDeviceAuths(deviceProductAuthList);
                    if (deviceProductAuthList != null && deviceProductAuthList.size() > 0) {
                        deviceInfoWxpubCacheDto.setExpireTime(deviceProductAuthList.get(0).getExpireTime());
                    }
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoWxpubCacheDto)));
            } else {//新增
                DeviceInfoWxpubCacheDto deviceInfoWxpubCacheDto = new DeviceInfoWxpubCacheDto();
                deviceInfoWxpubCacheDto.setMerId(wxpubInfoEntity.getMerId());
                deviceInfoWxpubCacheDto.setWxAppId(wxpubInfoEntity.getWxAppId());
                deviceInfoWxpubCacheDto.setWxAppSecret(wxpubInfoEntity.getWxAppSecret());
                deviceInfoWxpubCacheDto.setTarget(wxpubInfoEntity.getTarget());
                deviceInfoWxpubCacheDto.setConfigId(wxpubInfoEntity.getConfigId());
                deviceInfoWxpubCacheDto.setDeviceId(wxpubInfoEntity.getDeviceId());
                deviceInfoWxpubCacheDto.setDeviceUniqueId(wxpubInfoEntity.getId());
                deviceInfoWxpubCacheDto.setMsgHandler(DeviceTypeEnum.WX_PUB.getId());
                deviceInfoWxpubCacheDto.setDeviceName(wxpubInfoEntity.getDeviceName());
                deviceInfoWxpubCacheDto.setDeviceRemark(wxpubInfoEntity.getDeviceRemark());
                deviceInfoWxpubCacheDto.setHeadImgUrl(wxpubInfoEntity.getHeadImgUrl());
                deviceInfoWxpubCacheDto.setDeviceAuths(deviceProductAuthList);
                if (deviceProductAuthList != null && deviceProductAuthList.size() > 0) {
                    deviceInfoWxpubCacheDto.setExpireTime(deviceProductAuthList.get(0).getExpireTime());
                }
                //更新PC端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoWxpubCacheDto)));
            }
        }
    }

    /**
     * 购买企业微信机器人授权-卡密支付
     *
     * @param deviceWxworkInfo
     * @author liuqi
     * @date 2021/8/25 11:38
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void buyWxworkDeviceAuthByCard(DeviceWxworkInfo deviceWxworkInfo, Integer deviceTypeId, Cdkeys cdkeys, Integer now, String trandeNo, List<String> productCodeList) {

        //修改卡密状态为使用
        cdkeys.setStatus(CdkeysStatusEnum.USED.getId());
        cdkeys.setUpdateTime(now);
        cdkeys.setTradeno(trandeNo);
        cdkeys.setUseTime(now);
        int i = cdkeysMapper.updateById(cdkeys);

        if (i == 0) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "修改卡密失败");
        }

        //卡授权类型
        Integer authType_card = 1;
        //时长
        Integer quantity_card = 0; //卡密支付，时长从卡信息获取
        Integer beginTime_card = now;
        Integer expireTime_card = now;
        if (cdkeys.getType().intValue() == 1) {//月卡
            quantity_card = 1;
            authType_card = DeviceAuthTypeEnum.DAT_MONTH.getId();
        } else if (cdkeys.getType().intValue() == 2) { //年卡
            quantity_card = 12;
            authType_card = DeviceAuthTypeEnum.DAT_YEAR.getId();
        }

        for (String productCode : productCodeList) {
            //添加机器人授权表-如果没有则新增，有则修改
            DeviceAuth deviceAuth = deviceAuthMapper.selectByDeviceIdAndProductCode(deviceWxworkInfo.getDeviceId(), productCode);
            if (deviceAuth != null) { //修改
                //判断旧到期时间是否过期
                Integer old_expireTime = Tools.getInteger(deviceAuth.getExpireTime());
                if (old_expireTime <= now) {//已经到期，则从今天开始算
                    beginTime_card = now;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                } else {
                    beginTime_card = old_expireTime;
                    expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                }
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setUpdateTime(now);
                deviceAuth.setAuthType(authType_card);
                deviceAuthMapper.updateById(deviceAuth);
            } else {
                deviceAuth = new DeviceAuth();
                deviceAuth.setId(IdWorker.getId());
                deviceAuth.setDeviceId(deviceWxworkInfo.getDeviceId());
                deviceAuth.setDeviceUniqueId(deviceWxworkInfo.getId());
                deviceAuth.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
                deviceAuth.setDeviceTypeId(DeviceTypeEnum.DT_WORK_WECHAT.getId());
                deviceAuth.setAuthType(authType_card);
                deviceAuth.setProductCode(productCode);
                beginTime_card = now;
                expireTime_card = beginTime_card + quantity_card * 30 * 24 * 3600;
                deviceAuth.setExpireTime(expireTime_card);
                deviceAuth.setCreateTime(now);
                deviceAuth.setUpdateTime(now);
                deviceAuthMapper.insert(deviceAuth);
            }

            //添加机器人授权明细表
            DeviceAuthDetail deviceAuthDetail = new DeviceAuthDetail();
            deviceAuthDetail.setId(IdWorker.getId());
            deviceAuthDetail.setMerId(deviceWxworkInfo.getMerId());
            deviceAuthDetail.setDeviceUniqueId(deviceWxworkInfo.getId());
            deviceAuthDetail.setDeviceId(deviceWxworkInfo.getDeviceId());
            deviceAuthDetail.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
            deviceAuthDetail.setDeviceTypeId(DeviceTypeEnum.DT_WORK_WECHAT.getId());
            deviceAuthDetail.setProductCode(productCode);
            deviceAuthDetail.setAuthType(authType_card);
            deviceAuthDetail.setQuantity(quantity_card);
            deviceAuthDetail.setBeginTime(beginTime_card);
            deviceAuthDetail.setExpireTime(expireTime_card);
            deviceAuthDetail.setPayTime(now);
            deviceAuthDetail.setPayType(PaymentTypeEnum.PAY_CARDID.getId());
            deviceAuthDetail.setTradeno(trandeNo);
            deviceAuthDetail.setAmount(0.00D);
            deviceAuthDetail.setRemark("卡密支付");
            deviceAuthDetail.setCardId(cdkeys.getCard());
            deviceAuthDetail.setCreateTime(now);
            deviceAuthDetail.setUpdateTime(now);
            deviceAuthDetailMapper.insert(deviceAuthDetail);
        }

        //封装机器人授权信息
        List<DeviceInfoBaseCache.DeviceProductAuth> deviceProductAuthList = new ArrayList<>();
        //查询该机器人的所有产品授权信息
        DeviceAuthQuery deviceAuthQuery = new DeviceAuthQuery();
        deviceAuthQuery.setDeviceId(deviceWxworkInfo.getDeviceId());
        List<DeviceAuth> deviceAuthList = deviceAuthMapper.findByList(deviceAuthQuery);
        if (deviceAuthList != null && deviceAuthList.size() > 0) {
            for (DeviceAuth deviceAuth : deviceAuthList) {
                //机器人redis缓存信息-产品授权
                DeviceInfoBaseCache.DeviceProductAuth deviceProductAuth = new DeviceInfoBaseCache.DeviceProductAuth();
                deviceProductAuth.setAuthType(deviceAuth.getAuthType());
                deviceProductAuth.setExpireTime(deviceAuth.getExpireTime());
                deviceProductAuth.setProductCode(deviceAuth.getProductCode());
                deviceProductAuthList.add(deviceProductAuth);
            }

            //更新redis
            Object object_pc = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
            if (object_pc != null) { //修改
                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(object_pc.toString(), DeviceInfoWxWorkCacheDto.class);
                if (deviceInfoWxWorkCacheDto != null) {
                    deviceInfoWxWorkCacheDto.setDeviceAuths(deviceProductAuthList);
                    //更新企业微信机器人redis
                    redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoWxWorkCacheDto)));
                }
            } else {//新增
                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = new DeviceInfoWxWorkCacheDto();
                deviceInfoWxWorkCacheDto.setMerId(deviceWxworkInfo.getMerId());
                deviceInfoWxWorkCacheDto.setDeviceId(deviceWxworkInfo.getDeviceId());
                deviceInfoWxWorkCacheDto.setDeviceUniqueId(deviceWxworkInfo.getId());
                deviceInfoWxWorkCacheDto.setConfigId(deviceWxworkInfo.getConfigId());
                deviceInfoWxWorkCacheDto.setDeviceAuths(deviceProductAuthList);
                deviceInfoWxWorkCacheDto.setMsgHandler(DeviceTypeEnum.DT_WORK_WECHAT_VALUE); //接收消息
                if (deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT.getId()) { //PC端接收消息
                    deviceInfoWxWorkCacheDto.setMsgHandler(1); //接收消息
                } else {
                    deviceInfoWxWorkCacheDto.setMsgHandler(0); //不接受消息
                }
                //更新PC端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoWxWorkCacheDto)));
            }

            //更新移动端redis
            Object object_moblie = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
            if (object_moblie != null) { //修改
                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(object_moblie.toString(), DeviceInfoMobileWxWorkCacheDto.class);
                if (deviceInfoMobileWxWorkCacheDto != null) {
                    deviceInfoMobileWxWorkCacheDto.setDeviceAuths(deviceProductAuthList);
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoMobileWxWorkCacheDto)));

            } else {
                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = new DeviceInfoMobileWxWorkCacheDto();
                deviceInfoMobileWxWorkCacheDto.setMerId(deviceWxworkInfo.getMerId());
                deviceInfoMobileWxWorkCacheDto.setDeviceId(deviceWxworkInfo.getDeviceId());
                deviceInfoMobileWxWorkCacheDto.setDeviceUniqueId(deviceWxworkInfo.getId());
                deviceInfoMobileWxWorkCacheDto.setConfigId(deviceWxworkInfo.getConfigId());
                deviceInfoMobileWxWorkCacheDto.setDeviceAuths(deviceProductAuthList);
                if (deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_ANDROID_WORK_WECHAT.getId()) { //手机端接收消息
                    deviceInfoMobileWxWorkCacheDto.setMsgHandler(1);
                } else {
                    deviceInfoMobileWxWorkCacheDto.setMsgHandler(0);
                }
                //更新移动端机器人redis
                redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()), Tools.getStrEmpty(JSONObject.toJSONString(deviceInfoMobileWxWorkCacheDto)));
            }
        }

        //异步执行给机器人移动端APP授权
        deviceWechatInfoSyncService.mobiledeviceAuthAddSync(deviceWxworkInfo.getDeviceId(), deviceWxworkInfo.getRecvmsgType());
    }
}
